/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
package vn.demand.dragon.view
{
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.controls.Alert;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.events.CloseEvent;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.MarketingApplicationFacade;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.QuotationProxy;
	import vn.demand.dragon.view.components.QuotationList;
    
    /**
     * A Mediator for interacting with the EmployeeList component
     */
    public class QuotationListMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "QuotationListMediator";
        
        public var id:String = null;
        
        /**
         * Constructor. 
         */
        public function QuotationListMediator(viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );
			
			quotationProxy = QuotationProxy( facade.retrieveProxy( QuotationProxy.NAME ) );
			masterdataProxy = MasterdataProxy( facade.retrieveProxy( MasterdataProxy.NAME ) );
			
			quotationList.addEventListener(QuotationList.SELECT_QUOTATION, selectQuotation);
			quotationList.addEventListener(QuotationList.NEW_QUOTATION, newQuotation);
			quotationList.addEventListener(QuotationList.DELETE_QUOTATION, deleteQuotation);
			quotationList.addEventListener(QuotationList.CLOSE_QUOTATIONS_LIST, closeQuotationList);
			quotationList.addEventListener(QuotationList.REFRESH_QUOTATIONS_LIST, onRefreshQuotationList);
			
			quotationList.dgQuotation.columns[3].labelFunction = lfQuotationStatus; 
			
        }

		public function newQuotation(event:Event):void {
			sendNotification(MarketingApplicationFacade.NEW_QUOTATION, null);
		}
		
		public function deleteQuotation(event:Event):void {
			var sel = quotationList.dgQuotation.selectedItem;
			if (sel == null) {
				alert.error("You do not select any quotation to delete!");
				return;
			}
			alert.confirm("Do you want to remove?", function(e:CloseEvent) {
    		if (e.detail == Alert.NO) {
        			return;
        		}
				sendNotification(MarketingApplicationFacade.DELETE_QUOTATION, sel);
        	});
		}
		
		public function onRefreshQuotationList(event:Event):void {
			doRefreshQuotationList();
		}
		
		public function doRefreshQuotationList():void {
			sendNotification(CommonCommand.SEARCH_QUOTATION_RESET);
		}

		public function selectQuotation(event:Event):void {
			if (quotationList.dgQuotation.selectedIndex < 0) {
				alert.error("You should select a quotation");
				return;
			}
			sendNotification(MarketingApplicationFacade.QUOTATION_SELECTED, quotationList.dgQuotation.selectedItem);
		}
		
		public function closeQuotationList(event:Event):void {
			sendNotification(MarketingApplicationFacade.CLOSE_QUOTATIONS_LIST, quotationList);
		}

        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [ MarketingApplicationFacade.LOAD_QUOTATION_SUCCESS,            
					MarketingApplicationFacade.LOAD_QUOTATION_FAILED,
					MarketingApplicationFacade.BROWSE_QUOTATION_LIST,
					MarketingApplicationFacade.QUOTATION_UPDATED,
					MarketingApplicationFacade.LOAD_MASTERDATA_SUCCESS,
					CommonCommand.DELETE_QUOTATION_SUCESSFUL,
					CommonCommand.DELETE_QUOTATION_FAILED,
			];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
                case MarketingApplicationFacade.LOAD_QUOTATION_SUCCESS:
                    quotationList.dgQuotation.dataProvider = quotationProxy.quotationListDP;
                    break;
				case MarketingApplicationFacade.LOAD_QUOTATION_FAILED:
					quotationList.dgQuotation.errorString = quotationProxy.errorStatus;
					break;
				case MarketingApplicationFacade.QUOTATION_UPDATED:
					quotationProxy.loadQuotations();
					break;
				case MarketingApplicationFacade.LOAD_MASTERDATA_SUCCESS:
					quotationProxy.loadQuotations();
					break;
				case CommonCommand.DELETE_QUOTATION_SUCESSFUL:
					alert.info("Delete quotation sucessfull");
					break;
				case CommonCommand.DELETE_QUOTATION_FAILED:
					alert.error(note.getBody() as String);
					break;
            }
        }
                    
        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return EmployeeList the viewComponent cast to EmployeeList
         */
        protected function get quotationList():QuotationList
		{
            return viewComponent as QuotationList;
        }
		
//		private function logout( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.APP_LOGOUT );
//		}
//		
//		private function addEmployee( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.ADD_EMPLOYEE );
//		}
//		
//		private function updateEmployee( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.UPDATE_EMPLOYEE, quotationList.employees_li.selectedItem);
//		}
		
		public function lfQuotationStatus(item:Object, column:DataGridColumn):String {
        	return masterdataProxy.getQuotationStatusById(item[column.dataField]);
        }
		
		private var quotationProxy:QuotationProxy;
		private var masterdataProxy:MasterdataProxy;
    }
}